# coding:utf-8
import ConfigParser
import ast
class Config:
    tmp_cookies = ''
    weiboId = ''
    updateCycle = ''
    updateNum = ''
    first_time = True

    classify = ''
    medium = ''
    media = ''
    company = ''
    province = ''

    host = ''
    user = ''
    passwd = ''
    db_proxy = ''
    db_data = ''

    medias = []
    companys = []
    provinces = []
    citys = []
    weiboids = []


    def __init__(self, filepath):
        print "read config..."
        self.filepath = filepath
        self.cp = ConfigParser.SafeConfigParser()
        self.cp.read(filepath)


    def readDb(self):
        if self.cp.has_option('db_conf', 'host') \
                and self.cp.has_option('db_conf', 'user') \
                and self.cp.has_option('db_conf','passwd')\
                and self.cp.has_option('db_conf', 'db_proxy') \
                and self.cp.has_option('db_conf', 'db_data'):

            Config.host = self.cp.get('db_conf', 'host')
            Config.user = self.cp.get('db_conf', 'user')
            Config.passwd = self.cp.get('db_conf', 'passwd')
            Config.db_proxy = self.cp.get('db_conf', 'db_proxy')
            Config.db_data = self.cp.get('db_conf', 'db_data')

            '''print 'Config.host ', Config.host
            print 'Config.user  ', Config.user
            print 'Config.passwd ',Config.passwd
            print ' Config.db_proxy ', Config.db_proxy
            print ' Config.db_data ',  Config.db_data'''

            return True
        else:
            return False

    def read(self):
        if self.cp.has_option('weibo', 'cookies') \
                and self.cp.has_option('update','updateCycle')\
                and self.cp.has_option('update', 'updateNum') \
                and self.cp.has_option('update', 'firstTime') \
                and self.cp.has_option('data', 'classify'):

            Config.tmp_cookies = self.cp.get('weibo', 'cookies')

            #Config.weiboId = self.cp.get('data', 'weiboids')
            Config.updateCycle = self.cp.get('update', 'updateCycle')
            Config.updateNum = self.cp.get('update', 'updateNum')

            #固定字段
            Config.classify = self.cp.get('data', 'classify')
            '''Config.medium = self.cp.get('data', 'medium')
            Config.media = self.cp.get('data', 'media')
            Config.company = self.cp.get('data', 'company')
            Config.province = self.cp.get('data', 'province')'''

            #print 'classify: ' + self.classify + 'medium: ' + self.medium + 'media: ' + self.media + 'company: ' + self.company + 'province' + self.province
            #print 'classify: ' + Config.classify + 'medium: ' + Config.medium + 'media: ' + Config.media + 'company: ' + Config.company + 'province' + Config.province

            if int(self.cp.get('update', 'firstTime')) == 1:
                Config.first_time = True
                self.cp.set('update','firstTime','0')
                self.cp.write(open(self.filepath, "w"))

            else:
                Config.first_time = False
            print 'first_time', Config.first_time
            print 'tep_cookies: ', Config.tmp_cookies
            print 'weiboId',Config.weiboId
            print 'updateCycle', Config.updateCycle
            print 'updateNum', Config.updateNum
            return True

        else:
            return False

    def readWeibo(self):
        if self.cp.has_option('data', 'medias') \
            and self.cp.has_option('data', 'companys') \
            and self.cp.has_option('data', 'provinces') \
            and self.cp.has_option('data', 'citys')  \
            and self.cp.has_option('data', 'weiboids') :

            Config.medias = ast.literal_eval(self.cp.get('data', 'medias'))
            Config.companys = ast.literal_eval(self.cp.get('data', 'companys'))
            Config.provinces = ast.literal_eval(self.cp.get('data', 'provinces'))
            Config.citys = ast.literal_eval(self.cp.get('data', 'citys'))
            Config.weiboids = ast.literal_eval(self.cp.get('data', 'weiboids'))


            #print medias

            print type(Config.medias)
            print len(Config.medias)
            for i in  range(0, len(Config.medias)):
                print 'media' + str(i) + Config.medias[i]

            for i in  range(0, len(Config.companys)):
                print 'companys' + str(i) + Config.companys[i]

            for i in range(0, len(Config.provinces)):
                print 'provinces' + str(i) + Config.provinces[i]

            for i in  range(0, len(Config.citys)):
                print 'citys' + str(i) + Config.citys[i]

            for i in range(0, len(Config.weiboids)):
                print 'weiboids' + str(i) + Config.weiboids[i]


            return True
        else:
            return False
Config('spider.conf').readWeibo()